// pages/new-product-detail/new-product-detail.js
import { UserInfo } from "../../models/userinfo";
import { Factory } from "../../models/factory";
import { Article } from "../../models/article";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    userInfo: null,
    priceaAndMcArr: [],
    id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = new UserInfo();
    // this.data.userInfo = userInfo.getUserInfo();
    this.setData({
      userInfo: userInfo.getUserInfo()
      // info: JSON.parse(options.item)
    });
    this.data.id = options.id;
    console.log(this.data.info);
    this.init();
    // if (this.data.info.PriceaAndMc) {
    //   this.setData({
    //     priceaAndMcArr: this.handlePriceaAndMc(this.data.info.PriceaAndMc)
    //   });
    // }
  },

  async init() {
    const data = await Article.GetNoticeData({ Id: this.data.id });
    if (data && data.Data) {
      data.Data.time = data.Data.CreateTime.split(".")[0];
      this.setData({
        info: data.Data
      });
    }
  },

  handlePriceaAndMc(str) {
    return str.split("|").map(item => {
      return item.split(":");
    });
  },

  async viewFactory() {
    let factoryId = await this.getFactoryIdByUserId(this.data.info.User_id);
    wx.navigateTo({
      url: `/pages/manufacturer-detail/manufacturer-detail?id=${factoryId}`
    });
  },
  // 根据userId查工厂Id
  async getFactoryIdByUserId(userId) {
    let postData = {
      Search: { all: true, userId: userId, keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };
    const data = await Factory.GetDataList(postData);
    if (data.Data.length > 0) {
      return data.Data[0].Id;
    }
  },

  canSee() {
    // popedomId
    // 普通:1290559541989085184
    // 会员:1290559635618533376
    if (
      this.data.userInfo.RegistType == 1 &&
      this.data.userInfo.PopedomId == "1290559635618533376"
    ) {
      return true;
    }
    if (
      this.data.userInfo.RegistType == 2 &&
      this.data.userInfo.Id == this.data.info.User_id
    ) {
      return true;
    }
    return false;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
